package GuessNumber;

/**
 * @Author mao
 * @Date 2025/11/18
 * @Description 线程同步类
 */
class NumberSync {
    private int targetNumber;
    // 数字是否以及生成
    private boolean numberGenerated = false;
    // 是否以及完成猜测
    private boolean gameCompleted = false;
    // 猜测次数
    private int guessCount = 0;

    public synchronized int getTargetNumber() {
        while (!numberGenerated && !gameCompleted) {
            try {
                wait();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
                break;
            }
        }
        return targetNumber;
    }

    public synchronized void setTargetNumber(int number) {
        this.targetNumber = number;
        this.numberGenerated = true;
        notifyAll();
    }

    public synchronized boolean isGameCompleted() {
        return gameCompleted;
    }

    public synchronized void setGameCompleted(boolean completed) {
        this.gameCompleted = completed;
        if (completed) {
            notifyAll();
        }
    }

    public synchronized void incrementGuessCount() {
        guessCount++;
    }

    public synchronized int getGuessCount() {
        return guessCount;
    }
}
